import Axios from "axios"
import * as constants from './constants'
import { fromJS } from 'immutable'

const changeHomeData = (result) => ({
    type: constants.CHANGE_HOME_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    recommendList: result.recommendList
})

const addHomeList = (result, nextPage) => ({
    type: constants.ADD_ARTICLE_LIST,
    list: fromJS(result),
    nextPage
})

export const getHomeInfo = () => {
    return (dispatch) => {
        Axios.get('api/home.json').then((res) => {
            const data = res.data.data
            dispatch(changeHomeData(data))
        }).catch(
            console.log('error')
        )
    }
}

export const getMoreList = (page) => {
    return (dispatch) => {
        Axios.get('api/homeList.json?page='+page).then((res) => {
            const data = res.data.data
            dispatch(addHomeList(data, page + 1))
        }).catch(
            console.log('error')
        )
    }
}

export const toggleTopShow = (show) => ({
    type: constants.TOGGLE_SCROLL_TOP,
    show
})